package com.dhcc.bpm.common.utils;


import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


/**
 * @ClassName SensitiveUtils
 * @Description 刷新敏感词工具类
 * @Author daina
 * @Date 2020/4/14 14:09
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Component
public class SensitiveUtils
{
    private static final Logger log = LoggerFactory.getLogger(SensitiveUtils.class);

    private static RedisMessageSender redisMessageSender;

    /** 门户后端地址 */
    private static String PORTAL_URL;
    /** 门户后端访问协议 */
    private static String PORTAL_ACCESS;
    /** 敏感词Key */
    private static String KEY;
    /** 刷新敏感词接口地址 */
    private static String SENSITIVE_URL;

    @Value("${bpm.portal-url}")
    public void setPortalUrl(String portalUrl) {
        PORTAL_URL = portalUrl;
    }
    @Value("${bpm.portal-access}")
    public void setPortalAccess(String portalAccess) {
        PORTAL_ACCESS = portalAccess;
    }
    @Value("${bpm.sensitive.key}")
    public void setKey(String key) {
        KEY = key;
    }
    @Value("${bpm.sensitive.sensitive-url}")
    public void setPortUrl(String portUrl) {
        SENSITIVE_URL = portUrl;
    }

    private static RedisMessageSender redisMessageSender(){
        if (redisMessageSender == null){
            redisMessageSender = SpringUtil.getBean("redisMessageSender");
        }
        return redisMessageSender;
    }

    /**
     * @Description --調門戶接口
     * @Author daina
     * @Date 16:08 2020/4/14
     * @param title
     * @return boolean
     **/
    public boolean refreshSensitiveWords_portal(String title) {
        RestTemplate restTemplate = new RestTemplate();
        Kv info = Kv.create();
        info.set("key",KEY);
        info.set("title",title);
        String result;
        try {
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(PORTAL_ACCESS+PORTAL_URL+SENSITIVE_URL, info, String.class);
        result = responseEntity.getBody();
        } catch (Exception e) {
            log.error("刷新敏感词异常：{}", e);
            return false;
        }
        if(result.contains("000000")){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @Description -- redis消息推送
     * @Author daina
     * @Date 16:08 2020/4/14
     * @param title
     * @return boolean
     **/
    public boolean refreshSensitiveWords(String title) {
        JSONObject info = new JSONObject();
        info.set("key", KEY);
        info.set("title", title);
        boolean bool = false;
        try {
            bool = redisMessageSender().produceMessage(ContentUtils.REFRESH_SENSITIVE, info);
        } catch (Exception e) {
            log.error("刷新敏感词异常：{}", e);
            return false;
        }

        return bool;
    }

}
